package com.ruoyi.experiment.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.experiment.domain.CtfExperimentalEnvironmentExample;
import com.ruoyi.experiment.domain.TeachingExperimentalEnvironment;
import com.ruoyi.experiment.mapper.TeachingExperimentalEnvironmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.experiment.mapper.TeachingExperimentalEnvironmentExampleMapper;
import com.ruoyi.experiment.domain.TeachingExperimentalEnvironmentExample;
import com.ruoyi.experiment.service.ITeachingExperimentalEnvironmentExampleService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 教学实验环境实例Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-29
 */
@Service
public class TeachingExperimentalEnvironmentExampleServiceImpl implements ITeachingExperimentalEnvironmentExampleService {
    @Resource
    private TeachingExperimentalEnvironmentExampleMapper teachingExperimentalEnvironmentExampleMapper;
    @Resource
    private TeachingExperimentalEnvironmentMapper teachingExperimentalEnvironmentMapper;

    /**
     * 查询教学实验环境实例
     *
     * @param teachingEnvironmentExampleId 教学实验环境实例主键
     * @return 教学实验环境实例
     */
    @Override
    public TeachingExperimentalEnvironmentExample selectTeachingExperimentalEnvironmentExampleByTeachingEnvironmentExampleId(Long teachingEnvironmentExampleId) {
        return teachingExperimentalEnvironmentExampleMapper.selectTeachingExperimentalEnvironmentExampleByTeachingEnvironmentExampleId(teachingEnvironmentExampleId);
    }

    /**
     * 查询教学实验环境实例列表
     *
     * @param teachingExperimentalEnvironmentExample 教学实验环境实例
     * @return 教学实验环境实例
     */
    @Override
    public List<TeachingExperimentalEnvironmentExample> selectTeachingExperimentalEnvironmentExampleList(TeachingExperimentalEnvironmentExample teachingExperimentalEnvironmentExample) {
        return teachingExperimentalEnvironmentExampleMapper.selectTeachingExperimentalEnvironmentExampleList(teachingExperimentalEnvironmentExample);
    }

    /**
     * 新增教学实验环境实例
     *
     * @param teachingExperimentalEnvironmentExample 教学实验环境实例
     * @return 结果
     */
    @Override
    public int insertTeachingExperimentalEnvironmentExample(TeachingExperimentalEnvironmentExample teachingExperimentalEnvironmentExample) {
        return teachingExperimentalEnvironmentExampleMapper.insertTeachingExperimentalEnvironmentExample(teachingExperimentalEnvironmentExample);
    }

    /**
     * 修改教学实验环境实例
     *
     * @param teachingExperimentalEnvironmentExample 教学实验环境实例
     * @return 结果
     */
    @Override
    public int updateTeachingExperimentalEnvironmentExample(TeachingExperimentalEnvironmentExample teachingExperimentalEnvironmentExample) {
        return teachingExperimentalEnvironmentExampleMapper.updateTeachingExperimentalEnvironmentExample(teachingExperimentalEnvironmentExample);
    }

    /**
     * 批量删除教学实验环境实例
     *
     * @param teachingEnvironmentExampleIds 需要删除的教学实验环境实例主键
     * @return 结果
     */
    @Override
    public int deleteTeachingExperimentalEnvironmentExampleByTeachingEnvironmentExampleIds(Long[] teachingEnvironmentExampleIds) {
        return teachingExperimentalEnvironmentExampleMapper.deleteTeachingExperimentalEnvironmentExampleByTeachingEnvironmentExampleIds(teachingEnvironmentExampleIds);
    }

    /**
     * 删除教学实验环境实例信息
     *
     * @param teachingEnvironmentExampleId 教学实验环境实例主键
     * @return 结果
     */
    @Override
    public int deleteTeachingExperimentalEnvironmentExampleByTeachingEnvironmentExampleId(Long teachingEnvironmentExampleId) {
        return teachingExperimentalEnvironmentExampleMapper.deleteTeachingExperimentalEnvironmentExampleByTeachingEnvironmentExampleId(teachingEnvironmentExampleId);
    }

    /**
     * 重做实验
     */
    @Override
    public void resetExperiment(Long teeId) {
        TeachingExperimentalEnvironmentExample example = teachingExperimentalEnvironmentExampleMapper.selectTeachingExperimentalEnvironmentExampleByTeachingEnvironmentExampleId(teeId);
        //从新设置实验开始和结束时间
        example.setExampleCreateTime(LocalDateTime.now());
        example.setExampleEndTime(LocalDateTime.now().plusHours(example.getEnvironment().getTeachingEnvironmentTime()));
        teachingExperimentalEnvironmentExampleMapper.updateTeachingExperimentalEnvironmentExample(example);
    }

    /**
     * 结束实验
     */
    @Override
    public void endExperiment(Long teeId) {
        TeachingExperimentalEnvironmentExample example = teachingExperimentalEnvironmentExampleMapper.selectTeachingExperimentalEnvironmentExampleByTeachingEnvironmentExampleId(teeId);
        //从新设置实验结束时间
        example.setExampleEndTime(LocalDateTime.now());
        teachingExperimentalEnvironmentExampleMapper.updateTeachingExperimentalEnvironmentExample(example);
    }

    /**
     * 增加实验时间
     */
    @Override
    @Transactional
    public void addExperimentTime(Long teeId) {
        TeachingExperimentalEnvironmentExample example = teachingExperimentalEnvironmentExampleMapper.selectTeachingExperimentalEnvironmentExampleByTeachingEnvironmentExampleId(teeId);
        TeachingExperimentalEnvironment tee = example.getEnvironment();
        //从新设置实验结束时间
        example.setExampleEndTime(example.getExampleEndTime().plusHours(tee.getAddTime()));
        //将环境的次数减一
        tee.setAddCount(tee.getAddCount() - 1);
        //更新
        teachingExperimentalEnvironmentExampleMapper.updateTeachingExperimentalEnvironmentExample(example);
        teachingExperimentalEnvironmentMapper.updateTeachingExperimentalEnvironment(tee);
    }
}
